/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadSetor.java
 *
 * Created on 14/10/2011, 18:50:07
 */
package visao;

import EnderecoEstoque.EnderecoEstoque;
import EnderecoEstoque.EnderecoEstoqueNaoCadastradoException;
import fachada.FachadaBDR;
import java.awt.Color;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import setor.Setor;
import util.CamposObrigatoriosException;
import util.ErroAcessoRepositorioException;
import util.KeyValue;

/**
 *
 * @author Administrador
 */
public class CadSetor extends javax.swing.JFrame {

    /** Creates new form CadSetor */
    public CadSetor() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/visao.gif")).getImage());

        // Setando os valores para zero e desabilitando os campos
        jTextField_QuantArmazenada.setEnabled(false);
        jCheckBox_Status.setSelected(true);
    }

    /** Creates new form CadSetor */
    public CadSetor(int idSetor) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/visao.gif")).getImage());
        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();

            Setor objSetor = objFachadaBDR.procurarSetor(idSetor);

            id_setor.setText(String.valueOf(objSetor.getIdSetor()));
            jTextField_DescricaoSetor.setText(objSetor.getDescricaoSetor());
            jTextField_QuantArmazenada.setText(String.valueOf(objSetor.getQuantidadeArmazenada()));
            jTextField_QuantMaxima.setText(String.valueOf(objSetor.getQuantidadeMaxima()));
            jTextField_QuantMinina.setText(String.valueOf(objSetor.getQuantidadeMinima()));
            jCheckBox_Status.setSelected(objSetor.getStatusSetor());
            jCheckBox_SubSetor.setSelected(objSetor.getFlagSubSetor());
            jComboBox_IdEndSetor.setSelectedItem(new KeyValue(objSetor.getDescricaoSetor(), Integer.toString(objSetor.getIdSetor())));


        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton_Ok = new javax.swing.JButton();
        jButton_Cancelar = new javax.swing.JButton();
        jLabel_Codigo1 = new javax.swing.JLabel();
        jLabel_Nome1 = new javax.swing.JLabel();
        jTextField_DescricaoSetor = new javax.swing.JTextField();
        jLabel_QuantArmazenada1 = new javax.swing.JLabel();
        jLabel_QuantMaxima1 = new javax.swing.JLabel();
        jLabel_QuantMinima1 = new javax.swing.JLabel();
        jCheckBox_SubSetor = new javax.swing.JCheckBox();
        jTextField_QuantArmazenada = new javax.swing.JTextField();
        jTextField_QuantMaxima = new javax.swing.JTextField();
        jTextField_QuantMinina = new javax.swing.JTextField();
        jCheckBox_Status = new javax.swing.JCheckBox();
        jLabel_EndSetor = new javax.swing.JLabel();
        jComboBox_IdEndSetor = new javax.swing.JComboBox();
        jLabel_Mensagem = new javax.swing.JLabel();
        id_setor = new javax.swing.JTextField();

        setTitle("Setor");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        jButton_Ok.setText("OK");
        jButton_Ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_OkActionPerformed(evt);
            }
        });

        jButton_Cancelar.setText("Cancelar");
        jButton_Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CancelarActionPerformed(evt);
            }
        });

        jLabel_Codigo1.setText("Codigo :");

        jLabel_Nome1.setText("Nome :");

        jLabel_QuantArmazenada1.setText("Quantidade Armazenada :");

        jLabel_QuantMaxima1.setText("Quantidade Maxima :");

        jLabel_QuantMinima1.setText("Quantidade Minima :");

        jCheckBox_SubSetor.setText("Sub-Setor");
        jCheckBox_SubSetor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox_SubSetorActionPerformed(evt);
            }
        });

        jTextField_QuantArmazenada.setText("0");
        jTextField_QuantArmazenada.setEnabled(false);

        jCheckBox_Status.setSelected(true);
        jCheckBox_Status.setText("Ativo");

        jLabel_EndSetor.setText("Endereço Setor :");

        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();

            List<EnderecoEstoque> listEndEstoque = objFachadaBDR.listarEnderecoEstoque("");

            jComboBox_IdEndSetor.addItem(new KeyValue("--- Favor Selecionar ---","0"));
            for (EnderecoEstoque objEndEstoque : listEndEstoque) {
                jComboBox_IdEndSetor.addItem(new KeyValue(objEndEstoque.getRuaEstoque(), Integer.toString(objEndEstoque.getIdEndereco())));
            }

        } catch (Exception e) {
        }

        id_setor.setText("0");
        id_setor.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel_Nome1)
                            .addComponent(jLabel_EndSetor)
                            .addComponent(jLabel_Codigo1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField_DescricaoSetor)
                            .addComponent(jComboBox_IdEndSetor, 0, 371, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(id_setor, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jCheckBox_Status)
                                .addGap(63, 63, 63))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel_QuantArmazenada1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel_QuantMaxima1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButton_Ok, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel_QuantMinima1)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton_Cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(27, 27, 27)
                                .addComponent(jLabel_Mensagem, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jTextField_QuantMaxima, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
                                    .addComponent(jTextField_QuantArmazenada, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
                                    .addComponent(jTextField_QuantMinina))
                                .addGap(99, 99, 99)
                                .addComponent(jCheckBox_SubSetor)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_Codigo1)
                    .addComponent(id_setor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBox_Status))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField_DescricaoSetor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_Nome1))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox_IdEndSetor, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_EndSetor))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addComponent(jLabel_QuantArmazenada1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(jTextField_QuantArmazenada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(jLabel_QuantMaxima1))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField_QuantMaxima, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField_QuantMinina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jCheckBox_SubSetor))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jLabel_QuantMinima1)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_Mensagem, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton_Ok)
                            .addComponent(jButton_Cancelar))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCheckBox_SubSetorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox_SubSetorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCheckBox_SubSetorActionPerformed

    private void jButton_CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CancelarActionPerformed
        // TODO add your handling code here:

        int cancelar = JOptionPane.showConfirmDialog(null, "Deseja cancelar ?");

        if (cancelar == JOptionPane.YES_OPTION) {

            dispose();

        } else {
        }


    }//GEN-LAST:event_jButton_CancelarActionPerformed

    private void jButton_OkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_OkActionPerformed
        // TODO add your handling code here:
        int idSetor = Integer.parseInt(id_setor.getText());
        String descricao = jTextField_DescricaoSetor.getText();
        int valorArmazenado = Integer.parseInt(jTextField_QuantArmazenada.getText());
        int valorMaximo = Integer.parseInt(jTextField_QuantMaxima.getText());
        int valorMinimo = Integer.parseInt(jTextField_QuantMinina.getText());
        boolean status = jCheckBox_Status.isSelected();
        boolean subSetor = jCheckBox_SubSetor.isSelected();
        int idEnderecoEstoque = Integer.parseInt(((KeyValue) jComboBox_IdEndSetor.getSelectedItem()).getValue());

        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();

            EnderecoEstoque endEstoque = objFachadaBDR.procurarEnderecoEstoque(idEnderecoEstoque);

            Setor setor = new Setor(idSetor, endEstoque, descricao, valorMaximo, valorMinimo, valorArmazenado, subSetor, status);

            if (idSetor == 0) {
                objFachadaBDR.cadastrarSetor(setor);
            } else {
                objFachadaBDR.atualizarSetor(setor);
            }

            String acao = (idSetor == 0) ? "Cadastrado" : "Atualizado";
            jLabel_Mensagem.setText("Setor " + acao + " com Sucesso");
            jLabel_Mensagem.setForeground(Color.BLACK);
            if (idSetor == 0) {

                jTextField_DescricaoSetor.setText("");
                jTextField_QuantArmazenada.setText("");
                jTextField_QuantMaxima.setText("");
                jTextField_QuantMinina.setText("");
                jCheckBox_Status.setSelected(false);
                jCheckBox_SubSetor.setSelected(false);
                jComboBox_IdEndSetor.setSelectedIndex(0);

            }
        } catch (CamposObrigatoriosException ex) {
            ex.printStackTrace();
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);
        } catch (EnderecoEstoqueNaoCadastradoException ex) {
            ex.printStackTrace();
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);
        } catch (ErroAcessoRepositorioException ex) {
            ex.printStackTrace();
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);
        }
    }//GEN-LAST:event_jButton_OkActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CadSetor().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField id_setor;
    private javax.swing.JButton jButton_Cancelar;
    private javax.swing.JButton jButton_Ok;
    private javax.swing.JCheckBox jCheckBox_Status;
    private javax.swing.JCheckBox jCheckBox_SubSetor;
    private javax.swing.JComboBox jComboBox_IdEndSetor;
    private javax.swing.JLabel jLabel_Codigo1;
    private javax.swing.JLabel jLabel_EndSetor;
    private javax.swing.JLabel jLabel_Mensagem;
    private javax.swing.JLabel jLabel_Nome1;
    private javax.swing.JLabel jLabel_QuantArmazenada1;
    private javax.swing.JLabel jLabel_QuantMaxima1;
    private javax.swing.JLabel jLabel_QuantMinima1;
    private javax.swing.JTextField jTextField_DescricaoSetor;
    private javax.swing.JTextField jTextField_QuantArmazenada;
    private javax.swing.JTextField jTextField_QuantMaxima;
    private javax.swing.JTextField jTextField_QuantMinina;
    // End of variables declaration//GEN-END:variables
}
